package cn.wen.core.refresh;

import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import cn.wen.base.adapter.recyclerview.QuickRecyclerViewHolder;

/**
 * RecyclerView头部和底部的包装Adapter
 */

public class WrapAdapter extends RecyclerView.Adapter {
    private RecyclerView.Adapter mAdapter;
    private static int BASE_ITEM_TYPE_HEADER = -100;
    private static int BASE_ITEM_TYPE_FOOTER = -200;
    private SparseArrayCompat<View> mHeaderViews = new SparseArrayCompat<>();
    private SparseArrayCompat<View> mFootViews = new SparseArrayCompat<>();

    public WrapAdapter(RecyclerView.Adapter adapter) {
        this.mAdapter = adapter;
    }

    public void addHeaderView(View view) {
        mHeaderViews.put(BASE_ITEM_TYPE_HEADER + mHeaderViews.size(), view);
    }

    public void removeHeaderView(View view) {
        int indexOfValue = mHeaderViews.indexOfValue(view);
        // 里面没有这个View
        if (indexOfValue != -1) {
            mHeaderViews.removeAt(indexOfValue);
            notifyDataSetChanged();
        }
    }

    public void addFooterView(View view) {
        // 里面没有这个View
        mFootViews.put(BASE_ITEM_TYPE_FOOTER + mFootViews.size(), view);
    }

    public void removeFooterView(View view) {
        int indexOfValue = mFootViews.indexOfValue(view);
        // 里面没有这个View
        if (indexOfValue != -1) {
            mFootViews.removeAt(indexOfValue);
            notifyDataSetChanged();
        }
    }

    private boolean isHeaderViewPos(int position) {
        return position < getHeadersCount();
    }

    private boolean isFooterViewPos(int position) {
        return position >= getHeadersCount() + getRealItemCount();
    }

    private int getRealItemCount() {
        return mAdapter.getItemCount();
    }

    public int getHeadersCount() {
        return mHeaderViews.size();
    }

    public int getFootersCount() {
        return mFootViews.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderViews.get(viewType) != null) {
            return new QuickRecyclerViewHolder(mHeaderViews.get(viewType));
        } else if (mFootViews.get(viewType) != null) {
            return new QuickRecyclerViewHolder(mFootViews.get(viewType));
        }
        return mAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        // 头部和底部不需要绑定数据
        if (isHeaderViewPos(position)) {
            return;
        } else if (isFooterViewPos(position)) {
            return;
        }
        mAdapter.onBindViewHolder(holder, position - getHeadersCount());
    }


    @Override
    public int getItemViewType(int position) {
        if (isHeaderViewPos(position)) {
            // 如果Position的位置在头部
            // 这里是从0开始
            return mHeaderViews.keyAt(position);
        } else if (isFooterViewPos(position)) {
            // 如果Position的位置在底部
            // 这里是从0开始
            return mFootViews.keyAt(position - getHeadersCount() - getRealItemCount());
        }
        // 正常的位置
        // 这里是从0开始
        return mAdapter.getItemViewType(position - getHeadersCount());
    }

    @Override
    public int getItemCount() {
        // 头部——正常——底部
        return getHeadersCount() + getRealItemCount() + getFootersCount();
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
            // 如果是头部和底部就占用SpanCount那个多个位置
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int viewType = getItemViewType(position);
                    if (mHeaderViews.get(viewType) != null || mFootViews.get(viewType) != null) {
                        return gridLayoutManager.getSpanCount();
                    } else if (spanSizeLookup != null) {
                        return spanSizeLookup.getSpanSize(position);
                    }
                    return 1;
                }
            });
            gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
        }
        mAdapter.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        int position = holder.getLayoutPosition();
        // 如果是头部和底部就合并单元格
        if (isHeaderViewPos(position) || isFooterViewPos(position)) {
            ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
            if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
                StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
                p.setFullSpan(true);
            }
            return;
        }
        mAdapter.onViewAttachedToWindow(holder);
    }
}
